package com.mh.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.skyway.spring.util.dao.AbstractJpaDao;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.mh.domain.Customer;

/**
 * DAO to manage Customer entities.
 * 
 */
@Repository("CustomerDAO")
@Transactional
public class CustomerDAOImpl extends AbstractJpaDao<Customer> implements
		CustomerDAO {

	/**
	 * Set of entity classes managed by this DAO.  Typically a DAO manages a single entity.
	 *
	 * 
	 * @generated
	 */
	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] { Customer.class }));
	/**
	 * EntityManager injected by Spring for persistence unit mhtest
	 *
	 * 
	 * @generated
	 */
	@PersistenceContext(unitName = "mhtest")
	private EntityManager entityManager;

	/**
	 * Instantiates a new CustomerDAOImpl
	 *
	 * 
	 * @generated
	 */
	public CustomerDAOImpl() {
		super();
	}

	/**
	 * Get the entity manager that manages persistence unit 
	 *
	 * 
	 * @generated
	 */
	public EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * Returns the set of entity classes managed by this DAO.
	 *
	 * 
	 * @generated
	 */
	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	/**
	 * JPQL Query - findCustomerByModifiedByContaining
	 *
	 */
	@Transactional
	public Set<Customer> findCustomerByModifiedByContaining(String modifiedBy) throws DataAccessException {

		return findCustomerByModifiedByContaining(modifiedBy, -1, -1);
	}

	/**
	 * JPQL Query - findCustomerByModifiedByContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Customer> findCustomerByModifiedByContaining(String modifiedBy, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCustomerByModifiedByContaining", startResult, maxRows, modifiedBy);
		return new LinkedHashSet<Customer>(query.getResultList());
	}

	/**
	 * JPQL Query - findCustomerByBirthday
	 *
	 */
	@Transactional
	public Set<Customer> findCustomerByBirthday(java.util.Date birthday) throws DataAccessException {

		return findCustomerByBirthday(birthday, -1, -1);
	}

	/**
	 * JPQL Query - findCustomerByBirthday
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Customer> findCustomerByBirthday(java.util.Date birthday, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCustomerByBirthday", startResult, maxRows, birthday);
		return new LinkedHashSet<Customer>(query.getResultList());
	}

	/**
	 * JPQL Query - findCustomerByFirstNameContaining
	 *
	 */
	@Transactional
	public Set<Customer> findCustomerByFirstNameContaining(String firstName) throws DataAccessException {

		return findCustomerByFirstNameContaining(firstName, -1, -1);
	}

	/**
	 * JPQL Query - findCustomerByFirstNameContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Customer> findCustomerByFirstNameContaining(String firstName, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCustomerByFirstNameContaining", startResult, maxRows, firstName);
		return new LinkedHashSet<Customer>(query.getResultList());
	}

	/**
	 * JPQL Query - findCustomerByModifiedOn
	 *
	 */
	@Transactional
	public Set<Customer> findCustomerByModifiedOn(java.util.Date modifiedOn) throws DataAccessException {

		return findCustomerByModifiedOn(modifiedOn, -1, -1);
	}

	/**
	 * JPQL Query - findCustomerByModifiedOn
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Customer> findCustomerByModifiedOn(java.util.Date modifiedOn, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCustomerByModifiedOn", startResult, maxRows, modifiedOn);
		return new LinkedHashSet<Customer>(query.getResultList());
	}

	/**
	 * JPQL Query - findCustomerByCityContaining
	 *
	 */
	@Transactional
	public Set<Customer> findCustomerByCityContaining(String city) throws DataAccessException {

		return findCustomerByCityContaining(city, -1, -1);
	}

	/**
	 * JPQL Query - findCustomerByCityContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Customer> findCustomerByCityContaining(String city, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCustomerByCityContaining", startResult, maxRows, city);
		return new LinkedHashSet<Customer>(query.getResultList());
	}

	/**
	 * JPQL Query - findCustomerById
	 *
	 */
	@Transactional
	public Customer findCustomerById(Integer id) throws DataAccessException {

		return findCustomerById(id, -1, -1);
	}

	/**
	 * JPQL Query - findCustomerById
	 *
	 */

	@Transactional
	public Customer findCustomerById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findCustomerById", startResult, maxRows, id);
			return (com.mh.domain.Customer) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findCustomerByCity
	 *
	 */
	@Transactional
	public Set<Customer> findCustomerByCity(String city) throws DataAccessException {

		return findCustomerByCity(city, -1, -1);
	}

	/**
	 * JPQL Query - findCustomerByCity
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Customer> findCustomerByCity(String city, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCustomerByCity", startResult, maxRows, city);
		return new LinkedHashSet<Customer>(query.getResultList());
	}

	/**
	 * JPQL Query - findCustomerByCreatedBy
	 *
	 */
	@Transactional
	public Set<Customer> findCustomerByCreatedBy(String createdBy) throws DataAccessException {

		return findCustomerByCreatedBy(createdBy, -1, -1);
	}

	/**
	 * JPQL Query - findCustomerByCreatedBy
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Customer> findCustomerByCreatedBy(String createdBy, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCustomerByCreatedBy", startResult, maxRows, createdBy);
		return new LinkedHashSet<Customer>(query.getResultList());
	}

	/**
	 * JPQL Query - findCustomerByFirstName
	 *
	 */
	@Transactional
	public Set<Customer> findCustomerByFirstName(String firstName) throws DataAccessException {

		return findCustomerByFirstName(firstName, -1, -1);
	}

	/**
	 * JPQL Query - findCustomerByFirstName
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Customer> findCustomerByFirstName(String firstName, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCustomerByFirstName", startResult, maxRows, firstName);
		return new LinkedHashSet<Customer>(query.getResultList());
	}

	/**
	 * JPQL Query - findCustomerByAddress
	 *
	 */
	@Transactional
	public Set<Customer> findCustomerByAddress(String address) throws DataAccessException {

		return findCustomerByAddress(address, -1, -1);
	}

	/**
	 * JPQL Query - findCustomerByAddress
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Customer> findCustomerByAddress(String address, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCustomerByAddress", startResult, maxRows, address);
		return new LinkedHashSet<Customer>(query.getResultList());
	}

	/**
	 * JPQL Query - findCustomerByCreatedByContaining
	 *
	 */
	@Transactional
	public Set<Customer> findCustomerByCreatedByContaining(String createdBy) throws DataAccessException {

		return findCustomerByCreatedByContaining(createdBy, -1, -1);
	}

	/**
	 * JPQL Query - findCustomerByCreatedByContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Customer> findCustomerByCreatedByContaining(String createdBy, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCustomerByCreatedByContaining", startResult, maxRows, createdBy);
		return new LinkedHashSet<Customer>(query.getResultList());
	}

	/**
	 * JPQL Query - findCustomerByEmail
	 *
	 */
	@Transactional
	public Set<Customer> findCustomerByEmail(String email) throws DataAccessException {

		return findCustomerByEmail(email, -1, -1);
	}

	/**
	 * JPQL Query - findCustomerByEmail
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Customer> findCustomerByEmail(String email, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCustomerByEmail", startResult, maxRows, email);
		return new LinkedHashSet<Customer>(query.getResultList());
	}

	/**
	 * JPQL Query - findCustomerByLastNameContaining
	 *
	 */
	@Transactional
	public Set<Customer> findCustomerByLastNameContaining(String lastName) throws DataAccessException {

		return findCustomerByLastNameContaining(lastName, -1, -1);
	}

	/**
	 * JPQL Query - findCustomerByLastNameContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Customer> findCustomerByLastNameContaining(String lastName, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCustomerByLastNameContaining", startResult, maxRows, lastName);
		return new LinkedHashSet<Customer>(query.getResultList());
	}

	/**
	 * JPQL Query - findCustomerByEmailContaining
	 *
	 */
	@Transactional
	public Set<Customer> findCustomerByEmailContaining(String email) throws DataAccessException {

		return findCustomerByEmailContaining(email, -1, -1);
	}

	/**
	 * JPQL Query - findCustomerByEmailContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Customer> findCustomerByEmailContaining(String email, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCustomerByEmailContaining", startResult, maxRows, email);
		return new LinkedHashSet<Customer>(query.getResultList());
	}

	/**
	 * JPQL Query - findCustomerByAddressContaining
	 *
	 */
	@Transactional
	public Set<Customer> findCustomerByAddressContaining(String address) throws DataAccessException {

		return findCustomerByAddressContaining(address, -1, -1);
	}

	/**
	 * JPQL Query - findCustomerByAddressContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Customer> findCustomerByAddressContaining(String address, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCustomerByAddressContaining", startResult, maxRows, address);
		return new LinkedHashSet<Customer>(query.getResultList());
	}

	/**
	 * JPQL Query - findCustomerByCreatedOn
	 *
	 */
	@Transactional
	public Set<Customer> findCustomerByCreatedOn(java.util.Date createdOn) throws DataAccessException {

		return findCustomerByCreatedOn(createdOn, -1, -1);
	}

	/**
	 * JPQL Query - findCustomerByCreatedOn
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Customer> findCustomerByCreatedOn(java.util.Date createdOn, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCustomerByCreatedOn", startResult, maxRows, createdOn);
		return new LinkedHashSet<Customer>(query.getResultList());
	}

	/**
	 * JPQL Query - findCustomerByPrimaryKey
	 *
	 */
	@Transactional
	public Customer findCustomerByPrimaryKey(Integer id) throws DataAccessException {

		return findCustomerByPrimaryKey(id, -1, -1);
	}

	/**
	 * JPQL Query - findCustomerByPrimaryKey
	 *
	 */

	@Transactional
	public Customer findCustomerByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findCustomerByPrimaryKey", startResult, maxRows, id);
			return (com.mh.domain.Customer) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findCustomerByLastName
	 *
	 */
	@Transactional
	public Set<Customer> findCustomerByLastName(String lastName) throws DataAccessException {

		return findCustomerByLastName(lastName, -1, -1);
	}

	/**
	 * JPQL Query - findCustomerByLastName
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Customer> findCustomerByLastName(String lastName, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCustomerByLastName", startResult, maxRows, lastName);
		return new LinkedHashSet<Customer>(query.getResultList());
	}

	/**
	 * JPQL Query - findCustomerByModifiedBy
	 *
	 */
	@Transactional
	public Set<Customer> findCustomerByModifiedBy(String modifiedBy) throws DataAccessException {

		return findCustomerByModifiedBy(modifiedBy, -1, -1);
	}

	/**
	 * JPQL Query - findCustomerByModifiedBy
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Customer> findCustomerByModifiedBy(String modifiedBy, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCustomerByModifiedBy", startResult, maxRows, modifiedBy);
		return new LinkedHashSet<Customer>(query.getResultList());
	}

	/**
	 * JPQL Query - findAllCustomers
	 *
	 */
	@Transactional
	public Set<Customer> findAllCustomers() throws DataAccessException {

		return findAllCustomers(-1, -1);
	}

	/**
	 * JPQL Query - findAllCustomers
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Customer> findAllCustomers(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllCustomers",startResult,maxRows);
		return new LinkedHashSet<Customer>(query.getResultList());
	}
	
	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Customer> findCustomers(Customer customer) throws DataAccessException {
		String queryFilter = "select myCustomer from Customer myCustomer where 1=1 ";
		
		if (customer.getId() != null && customer.getId().toString() !=""){
			queryFilter += "and myCustomer.id = " + customer.getId().toString() ;
		} else {
			if (customer.getAddress() != null && customer.getAddress() !=""){
				queryFilter += " and upper(myCustomer.address) like '%" + customer.getAddress().toString().toUpperCase() + "%'";
			}
			if (customer.getEmail() != null && customer.getEmail() !=""){
				queryFilter += " and upper(myCustomer.email) like '%" + customer.getEmail().toUpperCase() + "%'";;
			}
			
			if (customer.getFirstName() != null && customer.getFirstName() !=""){
				queryFilter += " and upper(myCustomer.firstName) like '%" + customer.getFirstName().toUpperCase() + "%'"; ;
			}
			if (customer.getBirthday() != null ){
				queryFilter += " and to_char(myCustomer.birthday,'dd-mm-yyyy') = '" +  customer.getBirthdayString() + "'"; ;
			}
			
			
			if (customer.getLastName() != null && customer.getLastName() !=""){
				queryFilter += " and upper(myCustomer.lastName) like '%" + customer.getLastName().toUpperCase() + "%'";
			}
			if (customer.getCity() != null && customer.getCity() !=""){
				queryFilter += " and upper(myCustomer.city) like '%" + customer.getCity().toUpperCase() + "%'";;
			}
			
		}
		
		return new LinkedHashSet<Customer>(entityManager.createQuery(queryFilter).getResultList());
		
	}
	

	/**
	 * Used to determine whether or not to merge the entity or persist the entity when calling Store
	 * @see store
	 * 
	 *
	 */
	public boolean canBeMerged(Customer entity) {
		return true;
	}
}

